import {
  Toast
} from "vant";
import cTitle from "components/title";

export default {
  data() {
    return {
      keyword: "",
      textArr: [], //总数组
      uploadArr: [], //传走
      aishow: false,
      ailist: [],
      wenti: ''
    };
  },

  components: {
    cTitle
  },
  activated() {
    if (!localStorage.getItem("uid")) {
      this.$router.push(this.fun.getUrl("member"));
      return
    }
    this.ai_problem();
  },
  computed: {

  },
  methods: {
    ai_problem() {
      $http.post("ZhengChengYc.ai_problem", {}).then((res) => {
        if (res.result == 1) {
          this.ailist = res.data;
        }
      });
    },
    ai_shiyishi(text) {
      this.keyword = text;
      this.wenti = text;
      this.submit()
    },
    submit: function (e) {
      if (this.keyword == '') {
        Toast("内容不能为空")
        return
      }
      var Arr = {
        role: 'user',
        content: this.keyword
      }
      this.uploadArr.push(Arr);
      this.textArr.push(Arr);
      this.wenti = this.keyword;
      this.keyword = '';
      this.aishow = true;
      var t = this;
      $http.post("ZhengChengYc.wenxinyiyan", {
        duihua: JSON.stringify(this.uploadArr)
      }).then((res) => {
        this.aishow = false;
        if (res.result == 1) {
          console.log(res, 'ai_problem')
          var newArr = {
            role: 'assistant',
            content: res.data != "" ? res.data : "服务器繁忙"
          }
          if (t.uploadArr.length >= 4) {
            t.uploadArr.shift()
            t.uploadArr.shift()
          }
          t.uploadArr.push(newArr)
          t.textArr.push(newArr)
          if (res.data != "") {
            t.setliaotianlog(t.wenti, res.data)
          }
        } else {
          Toast("系统繁忙")
        }
        t.keyword = '';
      });
    },
    rengong() {
      var t = this;
      $http.post("ZhengChengYc.shangji_mobile", {
        uid: localStorage.getItem("uid")
      }).then((res) => {
        if (res.data.mobile != 0) {
          var Arr = {
            role: 'assistant',
            content: res.data.text + ' ' + res.data.mobile
          }
          t.textArr.push(Arr)

        } else {
          Toast('暂无人工服务')
        }
      });

    },
    setliaotianlog(wenti, huida) {
      $http.post("ZhengChengYc.liaotianlog", {
        uid: localStorage.getItem("uid"),
        wenti: wenti,
        huida: huida
      }).then((res) => {
        if (res.result == 1) {

        }
      });

    },
    

  }
};